<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var map, control, centerLL
        watch = null,
        geolocation= {
            getCurrentPosition: function(f) {
                f({
                    coords: { latitude: 10, longitude: 10 }
                });
            },
            watchPosition: function(f) {
                watch = true;
            },
            clearWatch: function() {
                watch = null;
            }
        };

    function test_initialize(t) {
        t.plan(3);
        control = new OpenLayers.Control.Geolocate({geolocationOptions: {foo: 'bar'}});
        t.ok(control instanceof OpenLayers.Control.Geolocate,
             "new OpenLayers.Control returns object" );
        t.eq(control.displayClass, "olControlGeolocate", "displayClass is correct" );
        t.eq(control.geolocationOptions.foo, 'bar',
             'provided geolocation options are set in the geolocationOptions prop');
    }
    function test_bind(t) {
        t.plan(3);
        var control = new OpenLayers.Control.Geolocate({
            geolocation: geolocation
        });
        control.events.register('locationupdated', null, function() {
            t.ok(true, 'locationupdated event is fired when bound');
        });
        map.addControl(control);
        control.activate();
        var center = map.getCenter();
        t.eq(center.lon, 10, 'bound control sets the map lon');
        t.eq(center.lat, 10, 'bound control sets the map lat');
        control.deactivate();
        map.removeControl(control);
        map.setCenter(centerLL);
    }
    function test_unbind(t) {
        t.plan(3);
        var control = new OpenLayers.Control.Geolocate({
            geolocation: geolocation,
            bind: false
        });
        control.events.register('locationupdated', null, function() {
            t.ok(true, 'locationupdated event is fired when unbound');
        });
        map.addControl(control);
        control.activate();
        var center = map.getCenter();
        t.eq(center.lon, 0, 'unbound control doesnt sets the map lon');
        t.eq(center.lat, 0, 'unbound control doesnt sets the map lat');
        control.deactivate();
        map.removeControl(control);
        map.setCenter(centerLL);
    }
    function test_getCurrentLocation(t) {
        t.plan(5);
        var control = new OpenLayers.Control.Geolocate({
            geolocation: geolocation
        });
        map.addControl(control);
        t.eq(control.getCurrentLocation(), false, 'getCurrentLocation return false if control hasnt been activated');
        control.activate();
        map.setCenter(centerLL);
        t.eq(control.getCurrentLocation(), true, 'getCurrentLocation return true if control has been activated');
        var center = map.getCenter();
        t.eq(center.lon, 10, 'bound control sets the map lon when calling getCurrentLocation');
        t.eq(center.lat, 10, 'bound control sets the map lat when calling getCurrentLocation');
        control.deactivate();
        map.removeControl(control);
        map.setCenter(centerLL);
        var control2 = new OpenLayers.Control.Geolocate({
            geolocation: geolocation
        });
        map.addControl(control2);
        t.eq(control2.getCurrentLocation(), false, 'getCurrentLocation return false if control is in watch mode');
        control2.deactivate();
        map.removeControl(control2);
        map.setCenter(centerLL);
    }
    function test_watch(t) {
        t.plan(2);
        var control = new OpenLayers.Control.Geolocate({
            geolocation: geolocation,
            watch: true
        });
        map.addControl(control);
        control.activate();
        t.eq(watch, true, 'watch option makes calls to watchPosition');
        control.deactivate();
        t.eq(watch, null, 'deactivate control calls the clearwatch');
        map.removeControl(control);
        map.setCenter(centerLL);
    }
    function test_destroy(t) {
        t.plan(1);
        var control = new OpenLayers.Control.Geolocate({
            geolocation: geolocation,
            watch: true
        });
        control.activate();
        control.destroy();
        t.ok(control.active === false, "control deactivated before being destroyed");
    }

    function loader() {
        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("Test Layer",
            "http://labs.metacarta.com/wms-c/Basic.py?",
            {layers: "basic"});
        map.addLayer(layer);
        centerLL = new OpenLayers.LonLat(0,0);
        map.setCenter(centerLL, 5);
    }
  </script>
</head>
<body onload="loader()">
    <div id="map" style="width: 256px; height: 256px;"/>
</body>
</html>
